/*
 * Copyright (C), 2002-2016, 你的兔宝宝电子商务有限公司
 * FileName: ExcelUtils.java
 * Author:   你的兔宝宝
 * Date:     2016年7月19日 上午9:14:37
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.baby.demo.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * excel操作工具类
 * 
 * @author magic
 * 
 */
public class Excels extends ExcelBs{
    

    public static List<Map<String,Object>> read(String file, String[] pfields){
        return read4map(file, pfields);
    }
    
    public static List<Map<String,Object>> read(String file, String[] pfields,int offsetSheet){
        return read4map(file, pfields, -1, -1, offsetSheet, null, -1);
    }
    
    public static List<Map<String,Object>> read(String file, String[] pfields,String offsetSheetName){
        return read4map(file, pfields, -1, -1, -1, offsetSheetName, -1);
    }
    
    /**
     * 
     * @param file 文件
     * @param pfields 字段
     * @param offsetCol 偏移列:从0开始
     * @param offsetRow 偏移行:从0开始
     * @param offsetSheet 偏移表格地址:从0开始
     * @param offsetSheetName 偏移表格名称
     * @param headerRow 头行:从0开始
     * @param clazz 装备的数据类
     * @param table
     * @return
     */
    public static <T> List<T> read(String file, String[] pfields,int offsetCol,int offsetRow,int offsetSheet,String offsetSheetName,int headerRow,Class<T> clazz){
        List<Map<String,Object>> ol = read4map(file, pfields, offsetCol, offsetRow, offsetSheet, offsetSheetName, headerRow);
        return read4xls(ol,pfields,clazz);
    }
    
    public static <T> List<T> read(String file, String[] pfields,Class<T> clazz,int offsetSheet){
        List<Map<String,Object>> ol = read4map(file, pfields, -1, -1, offsetSheet, null, -1);
        return read4xls(ol,pfields,clazz);
    }
    
    public static <T> List<T> read(String file, String[] pfields,Class<T> clazz,String offsetSheetName){
        List<Map<String,Object>> ol = read4map(file, pfields, -1, -1, -1, offsetSheetName, -1);
        return read4xls(ol,pfields,clazz);
    }
    
    public static <T> List<T> read4xls(List<Map<String,Object>> olist, String[] pfields,Class<T> clazz){
        List<T> list = new ArrayList<T>();
        for (int i = 0; i <= olist.size(); i++) {
            Map<String,Object> map = olist.get(i);
            if( !Collections.isEmpty(map) ){
                list.add(Collections.map2obj(map, clazz));
            }
        }
        return list;
    }
    
    @SuppressWarnings("rawtypes")
    public static void main(String[] args) {
        /**
         * {
         *  file : '',
         *  fields : [{}],
         * }
         * 
         * 
         * */
        String file = "F:\\doc\\project\\自营PC装修市场\\doc\\装修表.xlsx";
        String[] fileds = new String[]{"name","title","memo","empty","dvalue"};
        String[] fieldDesc = new String[]{"名称",     "描述","备注",    "可能为空", "默认值"};
        List<Map<String, Object>> list = read(file,fileds,0);
        
        String header = "";
        for (int i = 0; i < fieldDesc.length; i ++) {
            String key = fieldDesc[i];
            header += key + "   ";
        }
        System.out.println(header);
        
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            Object obj = (Object) iterator.next();
            System.out.println(obj);
        }
        System.out.println("total data:   " + list.size());
    }
}